package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AllMomentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAllMomentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AllMomentLogic {
	return &AllMomentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AllMomentLogic) AllMoment(in *userrpc.AllMomentRequest) (*userrpc.AllMomentResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	//我要去看所有的朋友圈  包含我自己

	var allMoment []mysql.AllMoment

	mysql.DB.Raw("select moments.*, users.name from moments LEFT JOIN users on moments.user_id = users.id where user_id in (select friend_id from friends where user_id = ?  and deleted_at is null) or user_id = ?", in.UserId, in.UserId).Scan(&allMoment)

	fmt.Println(allMoment)
	//mysql.Friend 所有朋友在这里

	var momentLists []*userrpc.AllMomentList

	for _, moment := range allMoment {
		list := userrpc.AllMomentList{
			UserId:   moment.UserId,
			Context:  moment.Context,
			Address:  moment.Address,
			Resource: moment.Resource,
			Like:     moment.Like,
			Id:       int64(moment.ID),
			Name:     moment.Name,
		}

		momentLists = append(momentLists, &list)
	}

	return &userrpc.AllMomentResponse{List: momentLists}, nil
}
